
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>05-轮播图</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #outer {
            width: 420px;
            height: 319px;
            background-color: skyblue;

            margin: 50px auto;
            padding: 10px 0;

            position: relative;

            overflow: hidden;

        }

        #imgList {
            list-style: none;

            position: absolute;
            left: 0px;

        }

        #imgList li {
            float: left;

            margin: 0 10px;
        }

        #navDiv {
            position: absolute;
            bottom: 10px;
        }

        #navDiv a {
            display: inline-block;
            width: 10px;
            height: 10px;
            background-color: blue;
            opacity: .5;
        }

        #navDiv a:hover {
            background-color: black;
        }
    </style>

    <script>

        window.onload = function () {


            var imgList = document.getElementById("imgList");
            var imgArr = document.getElementsByTagName("img");
            //设置#imgList的宽度
            imgList.style.width = 420 * imgArr.length + "px";

            var outer = document.getElementById("outer");
            var navDiv = document.getElementById("navDiv");
            //设置navDiv居中
            navDiv.style.left = (outer.offsetWidth - navDiv.offsetWidth) / 2 + "px";

            //index表示当前图片的索引
            var index = 0;
            var allA = document.getElementsByTagName("a");


            /*点击超链接切换到指定的图片*/

            for (var i = 0; i < allA.length; i++) {

                //为每个超链接都添加一个num属性
                allA[i].num = i;

                allA[i].onclick = function () {

                    clearInterval(timer);

                    index = this.num

                    move(imgList, "left", -420 * index, 20, function () {
                        //开启自动切换
                        autoChange();
                    });

                    setA();
                }
            }
            //开启自动切换
            autoChange();

            //创建一个方法表示选中的a
            function setA() {

                //判断当前是否是最后一张图片
                if (index >= imgArr.length - 1) {

                    index = 0;
                    //将最后一张切换成最后一张
                    imgList.style.left = 0;
                }

                //遍历所有的a，设置颜色
                for (var i = 0; i < allA.length; i++) {
                    allA[i].style.backgroundColor = "";
                }

                //设置选中的a的颜色               
                allA[index].style.backgroundColor = "black";
            }

            var timer;
            //创建一个自动切换图片的方法
             function autoChange() {

                timer = setInterval(function () {

                    index++;

                    index %= imgArr.length;

                    move(imgList, "left", -420 * index, 20, function () {
                        setA()
                    });

                }, 1000);
            }

        }


        function move(obj, attr, target, speed, callback) {

            clearInterval(obj.timer);

            var current = parseInt(getStyle(obj, attr));

            if (current > target) {
                speed = -speed
            }

            obj.timer = setInterval(function () {

                var oldValue = parseInt(getStyle(obj, attr));

                var newValue = oldValue + speed;

                if ((speed < 0 && newValue < target) || (speed > 0 && newValue > target)) {
                    newValue = target;
                }

                obj.style[attr] = newValue + "px";

                if (newValue == target) {
                    clearInterval(obj.timer);

                    callback && callback();
                }

            }, 30);

        }


        function getStyle(obj, name) {
            if (window.getComputedStyle) {
                return getComputedStyle(obj, null)[name];
            } else {
                return obj.currentStyle[name];
            }
        }

    </script>
</head>

<body>

    <div id="outer">

        <ul id="imgList">
            <li><img src="./images/京东轮播图1.jpg" alt="" style="width: 400px;"></li>
            <li><img src="./images/京东轮播图2.jpg" alt="" style="width: 400px;"></li>
            <li><img src="./images/京东轮播图3.jpg" alt="" style="width: 400px;"></li>
            <li><img src="./images/京东轮播图4.jpg" alt="" style="width: 400px;"></li>
            <li><img src="./images/京东轮播图5.jpg" alt="" style="width: 400px;"></li>
            <li><img src="./images/京东轮播图6.jpg" alt="" style="width: 400px;"></li>
            <li><img src="./images/京东轮播图7.jpg" alt="" style="width: 400px;"></li>
            <li><img src="./images/京东轮播图8.jpg" alt="" style="width: 400px;"></li>
            <li><img src="./images/京东轮播图1.jpg" alt="" style="width: 400px;"></li>

        </ul>

        <div id="navDiv">
            <a href="###"></a>
            <a href="###"></a>
            <a href="###"></a>
            <a href="###"></a>
            <a href="###"></a>
            <a href="###"></a>
            <a href="###"></a>
            <a href="###"></a>
        </div>

    </div>

</body>

</html>